import { Component, OnInit, Host } from '@angular/core';
import { AppComponent } from '../../../app.component';
import { NotificationsService } from './notifications.service';
import { NotificationModel, NotificationStatus } from './model/notification.model';
import { ToasterService } from 'angular2-toaster';

@Component({
  templateUrl: './notifications.component.html',
  providers: [NotificationsService]
})
export class NotificationsComponent implements OnInit {
  public notification: NotificationModel;
  public notificationStatus: typeof NotificationStatus = NotificationStatus;
  public quillOptions: any = {
    toolbar: [
        ['bold', 'italic', 'underline', 'strike'],        // toggled buttons            
        [{ 'size': ['small', false, 'large', 'huge'] }],  // custom dropdown
        [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
        ['link']                         // link and image, video
    ]
  }

  constructor(@Host() private rootComponent: AppComponent,
  private notificationService: NotificationsService,
  private toasterService: ToasterService) { }
  
  ngOnInit() {
    this.notificationService.getNotification().subscribe(data=>{
      this.notification = data;
      this.rootComponent.finishLoading();
    });    
  }

  public publish() {
    this.notification.status = NotificationStatus.Published;
    this.update();
  }

  public unpublish() {
    this.notification.status = NotificationStatus.Unpublished;
    this.update();
  }

  private update(){
    this.notificationService.updateNotification(this.notification).subscribe(_=>{
      //update
      this.toasterService.pop('success', 'Success', 'Save operation executed successfully.');
    });
  }

}
